package com.taxi.driver.data.user.local;

import android.text.TextUtils;

import com.qianxx.network.RetrofitRequestTool;
import com.qianxx.utils.SP;
import com.qianxx.utils.TypeUtils;
import com.taxi.driver.data.entity.AssessmentEntity;
import com.taxi.driver.data.entity.BillEntity;
import com.taxi.driver.data.entity.CancelReasonEntity;
import com.taxi.driver.data.entity.CommentEntity;
import com.taxi.driver.data.entity.ComplainEntity;
import com.taxi.driver.data.entity.DriverEntity;
import com.taxi.driver.data.entity.EvaluateEntity;
import com.taxi.driver.data.entity.FeedBackEntity;
import com.taxi.driver.data.entity.HomeEntity;
import com.taxi.driver.data.entity.MessageEntity;
import com.taxi.driver.data.entity.ProblemEntity;
import com.taxi.driver.data.entity.UpdateMsg;
import com.taxi.driver.data.entity.WithdrawalDetailsEntity;
import com.taxi.driver.data.entity.WithdrawaleRecordEntity;
import com.taxi.driver.data.user.UserSource;
import com.taxi.driver.module.vo.AddressVO;

import org.litepal.crud.DataSupport;

import java.util.Collections;
import java.util.List;

import javax.inject.Inject;

import rx.Observable;

public class UserLocalSource implements UserSource {

    private SP mSP;
    private DriverEntity mUserInfo;
    private DriverEntity mTempDriverEntity;
    private String token;
    private String mProvince;
    private String mCity;
    private boolean mIsDisconnect;
    private static final String DRIVER_TYPE = "dirver_type";//司机类型
    private static final String ONGOING = "ongoing";//进行中订单
    private static final String DRIVER_INFO = "driver_info";//司机信息

    @Inject
    public UserLocalSource(SP sp) {
        mSP = sp;
    }

    @Override
    public Observable<String> reqVerifyCode(String phone) {
        return null;
    }

    @Override
    public Observable<DriverEntity> reqLogin(String phone, String code) {
        return null;
    }

    @Override
    public Observable<String> reqLogout(double onWorkLng, double onWorkLat, String uploadText) {
        token = null;
        return Observable.empty();
    }

    @Override
    public Observable<DriverEntity> getUserInfo() {
        return Observable.just(mUserInfo); // 可为空
    }

    @Override
    public void refreshUserInfo() {
        mUserInfo = null; //TODO
    }

    @Override
    public void setUserInfo(DriverEntity entity) {
        mUserInfo = entity; //TODO
    }

    @Override
    public boolean isLogin() {
        if (TextUtils.isEmpty(token)) {
            token = RetrofitRequestTool.getToken(mSP);
        }
        return !TextUtils.isEmpty(token);
    }

    @Override
    public int getCarLevelType() {
        return mUserInfo.carLevelType;
    }

    @Override
    public Observable<List<WithdrawaleRecordEntity>> widthdrawalRecord(int nowPage) {
        return null;
    }

    @Override
    public Observable<BillEntity> reqBills(int nowPage, int dateType, String start, String end, String type) {
        return null;
    }

    @Override
    public Observable<List<ProblemEntity>> getProblems() {
        return null;
    }

    @Override
    public Observable<List<FeedBackEntity>> getFeedbacks(int nowPage) {
        return null;
    }

    @Override
    public Observable<String> addFeedback(String title, String content) {
        return null;
    }

    @Override
    public Observable<WithdrawalDetailsEntity> getWithdrawalInfo(String cashUuid) {
        return null;
    }

    @Override
    public Observable<String> withdrawal(String cash, String collectAccount, int collectType, String password) {
        return null;
    }

    @Override
    public Observable<AssessmentEntity> reqAssessment(String startTime, String endTime) {
        return null;
    }

    @Override
    public Observable<EvaluateEntity> getEvaluates() {
        return null;
    }

    @Override
    public Observable<List<CommentEntity>> getYesterdayEvaluates(int nowPage) {
        return null;
    }

    @Override
    public Observable<String> resetPw(String newPsw, String oldPsw) {
        return null;
    }

    @Override
    public Observable<String> validate(String phone, String idcard, int driverType) {
        return null;
    }

    @Override
    public Observable<String> sendCode(String mobile) {
        return null;
    }

    @Override
    public Observable<String> verifyCode(String mobile, String identifyCode) {
        return null;
    }

    @Override
    public Observable<String> resetPwd(String mobile, String identifyCode, String idcard, String password, int driverType) {
        return null;
    }

    @Override
    public void setTempDriverEntity(DriverEntity entity) {
        //mTempDriverEntity = entity;
        mSP.putObject(DRIVER_INFO, entity);
    }

    @Override
    public DriverEntity getTempDriverEntity() {
        return mSP.getObject(DRIVER_INFO, DriverEntity.class);
    }

    @Override
    public Observable<HomeEntity> reqWorkInfo() {
        return null;
    }

    @Override
    public Observable<List<CancelReasonEntity>> reqCancelMsg() {
        return null;
    }

    @Override
    public Observable<List<ComplainEntity>> reqComplainMsg() {
        return null;
    }

    @Override
    public String getLocalDriverUuid() {
        if (mUserInfo == null) return "";
        return TypeUtils.getValue(mUserInfo.uuid);
    }

    @Override
    public List<AddressVO> getHistoryAddr() {
        // 需限定最多只能10个
        List<AddressVO> all = DataSupport.findAll(AddressVO.class);
        int deleteCounts = all.size() - 10;
        if (deleteCounts > 0) {
            for (int i = 0; i < deleteCounts; i++) {
                all.get(0).delete(); //注意：下标始终是0
                all.remove(0); //注意：下标始终是0
            }
        }
        Collections.reverse(all); //反转数据：存储时间早的数据在后
        return all;
    }

    @Override
    public void saveProvince(String province) {
        mProvince = province;
    }

    @Override
    public void saveCity(String city) {
        mCity = city;
    }

    @Override
    public String getProvince() {
        return mProvince;
    }

    @Override
    public String getCity() {
        return mCity;
    }

    @Override
    public void setNetworkStatus(boolean isDisconnect) {
        mIsDisconnect = isDisconnect;
    }

    @Override
    public Observable<List<MessageEntity>> getMsgList(int nowPage, int pageType) {
        return null;
    }

    @Override
    public Observable<UpdateMsg> updateMsg(String uuid, int status) {
        return null;
    }

    @Override
    public Observable<String> deleteAll() {
        return null;
    }

    @Override
    public Observable<String> deleteMsg(String uuid) {
        return null;
    }

    @Override
    public Observable<String> uploadErrorMsg(String errorType, String errorMsg) {
        return null;
    }
}